<?php

/**
 * manage actions.
 *
 * @package    worldmap
 * @subpackage manage
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class manageActions extends sfActions
{
  public function initForm()
  {
    $this->gMap = new GMap();
    $form = new sfGuardUserAdminForm($this->getUser()->getGuardUser());
    $fields = array('id', 'username', 'password', 'password_again');
    foreach ($form->getWidgetSchema()->getFields() as $name => $widget) {
      if (!in_array($name, $fields)) {
        $form->offsetUnset($name);
      }
    }

    $this->form->embedMergeForm('sfGuardUser', $form);
  }

  public function executeIndex(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated()) {
      $this->redirect('manage/edit');
    }
    $this->redirect('manage/new?type=User');
  }

  public function executeNew(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated()) {
      $this->executeIndex($request);
    }
    $this->type = $request->getParameter('type');
    $this->forward404Unless($this->type);
    $formClass = $this->type.'Form';
    $this->form = new $formClass();
    $this->initForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated()) {
      $this->executeIndex($request);
    }
    $this->forward404Unless($request->isMethod('post'));

    $this->type = $request->getParameter('type');
    $this->forward404Unless($this->type);
    $formClass = $this->type.'Form';
    $this->form = new $formClass();
    $this->initForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($entity = $this->getUser()->getGuardUser()->getEntity());
    $formClass = get_class($entity).'Form';
    $this->form = new $formClass($entity);
    $this->initForm();
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($entity = $this->getUser()->getGuardUser()->getEntity());
    $formClass = get_class($entity).'Form';
    $this->form = new $formClass($entity);
    $this->initForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($entity = $this->getUser()->getGuardUser()->getEntity());
    $entity->delete();

    $this->redirect('manage/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if (!$form->isValid()) {
      foreach ($form->getErrorSchema() as $field => $error) {
        printf(__LINE__." %s: %s\n", $field, $error->getMessage());
      }
      return;
    }

    $entity = $form->save();

    if (!$this->getUser()->isAuthenticated()) {
      $this->getUser()->signIn($entity->sfGuardUser);
    }
    $this->redirect('manage/edit');
  }
}
